package com.zk.lettcode;

/**
 * 罗马数字转整数
 * @Author: zhang kai
 * @Date: 2021/11/10
 * @Description: https://leetcode-cn.com/problems/roman-to-integer/
 */
public class Practice5 {
    public  int romanToInt(String s) {
        char[] c = s.toCharArray();
        int result = 0;
        for (int i = 0; i < c.length; i++) {
            if((i+1 < c.length) && switchChar(c[i] )< switchChar(c[i+1])) {
                result -= switchChar(c[i]);
            } else  {
                result += switchChar(c[i]);
            }
        }
        return result;
    }

    public static int switchChar(char c) {
        switch (c){
            case 'I' : return 1;
            case 'V' : return 5;
            case 'X' : return 10;
            case 'L' : return 50;
            case 'C' : return 100;
            case 'D' : return 500;
            case 'M' : return 1000;
            default: return 0;

        }
    }
}
